<?php
/**
 * Created by PhpStorm.
 * User: sundy
 * Date: 2020/2/3
 * Time: 10:11
 */

namespace App\Admin\Controllers;

use Illuminate\Support\Facades\DB;
use Encore\Admin\Controllers\AdminController;
use App\Record;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Encore\Admin\Show;

class RecordController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '记录查询';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Record);
        $grid->filter(function(Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->like('vc_product_name', 'Name');
//            $filter->like('nickname', 'User');
            $filter->between('dt_order_time', 'Order Time')->datetime();
        });
        $grid->quickSearch('out_trade_no','vc_account','i_product_id','vc_product_name','openid');
        $grid->i_id('ID')->hide();
        $grid->model()->orderBy('dt_order_time','desc');
        $grid->column('dt_order_time', __('Order Time'))->filter('range', 'datetime')->sortable();
        $grid->column('vc_account', __('Account'))->color('blue')->sortable();
        $grid->column('vc_imsi', __('IMSI'));
//        $grid->column('openid', __('OpenId'));
        $grid->column('', __('User'))->display(function () {
           $userInfo =  DB::table('wx_users')->where('openid',$this->openid)->first();
           if($userInfo){
               $url = config('app.url').'/admin/wx-user/'.$this->openid;
               return '<a href="'.$url.'" style="color: #000000"><div style="width: 120px;height: 30px;overflow: hidden;"><div style="float: left;width: 70px;">'.$userInfo->nickname.'</div><img src="'.$userInfo->headimgurl.'" style="width: 30px;height: 30px;padding-left:5px;float: right"></div></a>';
           }
        });
        $grid->column('out_trade_no', __('Out Trade No'));
        $grid->column('i_product_id', __('Product'));
        $grid->column('vc_product_name', __('Name'));
        $grid->column('product_price', __('Price'));
        $grid->column('amount', __('Amount'));
        $grid->column('ti_type', __('Type'))->display(function ($type) {
            return $type==1?'<span style="color:blue">充值</span>':'<span style="color:orange">购买</span>';
        })->filter(
            [
                1 => '充值',
                2 => '购买'
            ]
        );
        $grid->column('ti_pay_status', __('Pay Status'))->display(function ($ti_pay_status) {
            switch ($ti_pay_status) {
                case 1:
                    return '<span style="color:green">付款成功</span>';
                case 2:
                    return '<span style="color:red">失败</span>';
                case 0:
                    return '<span style="color:#777">已下单未付款</span>';
            }
        })->filter(
            [
                1 => '付款成功',
                2 => '失败',
                0 => '已下单未付款'
            ]
        );
        $grid->column('ti_status', __('Status'))->display(function ($ti_status) {
            if($ti_status===1){
                return '<span style="color:green">交易成功</span>';
            } else if($ti_status===0){
                return '<span style="color:red">交易失败</span>';
            } else if($ti_status===null){
                return '交易取消';
            }
        })->filter(
            [
                1 => '交易成功',
                null => '交易取消',
                0 => '交易失败'
            ]
        );
        $grid->column('ti_sign', __('Sign'))->display(function ($ti_sign) {
            $url = config('app.url').'/admin/record';
            switch ($ti_sign) {
                case 2:
                    return '<a href="'.$url.'/'.$this->i_id.'/edit"><span style="color:blue">需处理</span></a>';
                case 1:
                    return '<span style="color:green">已处理</span>';
            }
        })->filter(
            [
                2 => '需处理',
                1 => '已处理'
            ]
        );;
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            $actions->disableEdit();
        });
        $grid->tools(function ($tools) {
            $url = config('app.url').'/admin/record';
            $tools->append("<a href=$url class='btn btn-sm btn-success' style='float: right;margin-left: 20px;'>
            <i class='fa fa-save'></i>&nbsp;&nbsp;刷新
            </a>");
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        return $grid;
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Record);
        $form->text('vc_account','Account');
        $form->text('esim_url','eSim URL');
        $form->select('ti_status','Status')->options([0=>'交易失败',1=>'交易成功']);
        $form->select('ti_sign','Sign')->options([2=>'未处理',1=>'已处理']);
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();

        });
        return $form;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Record::findOrFail($id));
        $show->panel()->tools(function ($tools) {
            $tools->disableDelete();
        });
        $show->field('vc_account', __('Account'));
        $show->field('vc_imsi', __('IMSI'));
        $show->field('out_trade_no', __('Trade No'));
        $show->field('openid', __('OpenId'));
        $show->field('', __('User'))->unescape()->as(function () {
            $userInfo =  DB::table('wx_users')->where('openid',$this->openid)->first();
            if($userInfo){
                return $userInfo->nickname.'<span ><img src="'.$userInfo->headimgurl.'" style="width: 50px;height: 50px;padding: 5px;"></span>';
            }
        });
        $show->field('i_product_id', __('Product Id'));
        $show->field('vc_product_name', __('Name'));
        $show->field('i_valid_days', __('Valid Days'));
        $show->field('product_price', __('Price'));
        $show->field('ti_type', __('Type'));
        $show->ti_type('Type')->unescape()->as(function ($ti_type) {
            if ($ti_type == 1) {
                return '<span style="color:blue">充值</span>';
            }else{
                return '<span style="color:orange">购买</span>';
            }
        });

        $show->field('amount', __('Amount'));
        $show->field('dt_order_time', __('Order Time'));
        $show->ti_pay_status('Pay Status')->unescape()->as(function ($ti_pay_status) {
            if ($ti_pay_status == 1) {
                return '<span style="color:green">付款成功</span>';
            } else if($ti_pay_status == 2) {
                return '<span style="color:red">失败</span>';
            } else if($ti_pay_status == 0){
                return '<span style="color:#777">已下单未付款</span>';
            }
            return $ti_pay_status;
        });
        $show->ti_status('Status')->unescape()->as(function ($ti_status) {
            if ($ti_status === 1) {
                return '<span style="color:green">交易成功</span>';
            } else if($ti_status === null) {
                return '交易取消';
            } else if($ti_status === 0){
                return '<span style="color:red">交易失败</span>';
            }
        });

        $show->field('te_description', __('Description'));

        $show->esim_url('eSim URL')->unescape()->as(function ($esim_url) {

        return "<img src='{$esim_url}' />";

    });
        $show->field('esim_msg', __('eSim Msg'));
        return $show;
    }



}
